<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'城市站列表'),
);
$setting=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_paimerch_city',$params,array($pindex,$psize),$total,$fields,'','isone desc,id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['bossmid']);
		$item['pcd']=json_decode($item['pcd'],true);
		$item['pcd']=$item['pcd']['province'].''.$item['pcd']['city'].''.$item['pcd']['district'];
		$item['itemsum']=pdo_count('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'pcd like'=>'%'.$item['title'].'%'));
		$item['url']=custom_url('jy_paimerch/index//cityid='.$item['id'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：前端默认只显示本城市站的商户数据！','edit'=>true,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'title','title'=>'城市名称','type'=>'string'),
			array('name'=>'pcd','title'=>'城市地址','type'=>'string'),
			array('name'=>'member','title'=>'城主','type'=>'member'),
			array('name'=>'itemsum','title'=>'商户数','type'=>'string'),
			array('name'=>'url','title'=>'链接地址','type'=>'copy'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'setboss','title'=>'清空城主'),
			array('type'=>'href','op'=>'items','title'=>'商户列表'),
		),),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'城市名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setboss'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择城市！','error');
	}
	pdo_update('jy_paimerch_city',array('bossmid'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('清空成功！！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_paimerch_city',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['pcd'] = json_decode($item['pcd'],true);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回城市列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'城市名称','type'=>'string'),
			array('name'=>'bossmid','title'=>'选择城主','type'=>'choose','action'=>'core/member.get_member/'),
			array('name'=>'pcd','title'=>'所属地址','type'=>'pcd'),
			array('name'=>'status','title'=>'状态','type'=>'switch')
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='items'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(intval($_RQ['id'])){
		$city=pdo_getcolumn('jy_paimerch_city',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])),'title');
		$params['pcd like'] ='%'.$city.'%';
		$return['params']['id']=intval($_RQ['id']);
	}
	if(!empty($_RQ['merchname'])){
		$params['merchname like']='%'.trim($_RQ['merchname']).'%';
		$return['params']['merchname']=$_RQ['merchname'];
	}
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','merchname','mid','uniacid','pcd','logo','address','status','mobile','jointime');
	$list=pdo_getslice('jy_merch_user',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['pcd']=json_decode($item['pcd'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
			array('name'=>'member','title'=>'店主信息','type'=>'member'),
			array('name'=>'pcd.city','title'=>'城市站','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'jointime','title'=>'时间','type'=>'time'),
			
		)),
		'search'=>array(
			array('name'=>'merchname','width'=>15,'title'=>'商户名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status'),
		)
	);
	$return['status']=plugin('jy_merch')->merch_status;
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id=intval($postdata['id']);
	if(!$postdata['title']){
		exi('请填写主题名称！','error');
	}
	//print_r($postdata['content']);exit;
	$pcd = $postdata['pcd'];
	if(!$pcd['province']){
		exi('请完善城市站所属地址！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'bossmid'=>intval($postdata['bossmid']),
		'pcd'=>ijson_encode($postdata['pcd']),
		'status'=>intval($postdata['status']),
	);
	if($id){
		pdo_update('jy_paimerch_city',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_paimerch_city',$data);
	}
	exi('操作成功！','','list');
}